package com.ybbase.framework.controller.system;

import com.ybbase.framework.common.constant.CommonResultCode;
import com.ybbase.framework.model.dto.LogDTO;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.RestController;
import com.ybbase.framework.base.controller.BaseController;
import com.ybbase.framework.service.system.LogService;
import com.ybbase.framework.model.po.system.Log;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParams;
import com.ybbase.framework.base.model.ResponseBean;

/**
 * 系统日志 前端控制器
 *
 * @author cg
 * @since 2019-11-25
 * 云平台2.0
 */
@RestController
@RequestMapping(value = "/log", produces = {"application/json;charset=UTF-8"})
@Api("系统日志")
public class LogController extends BaseController {

    private static final Logger LOGGER = LogManager.getLogger(LogController.class);

    @Autowired
    private LogService targetService;

    /**
     * 系统日志分页查询
     *
     * @param pageIndex 页数
     * @param step      每页数量
     * @return ResponseBean
     * @author cg
     * @date 2019-11-25
     * @since cloud2.0
     */
    @GetMapping("/list")
    @ApiOperation(value = "系统日志分页查询", notes = "系统日志分页查询", response = ResponseBean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "rows", value = "每页行数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "log", value = "系统日志", dataType = "Log对象")
    })
    public ResponseBean findListByPage(@RequestParam(name = "current", defaultValue = "1") int pageIndex,
                                       @RequestParam(name = "size", defaultValue = "20") int step, LogDTO logDTO) {
        try {
            Page<Log> page = new Page(pageIndex, step);
            if (logDTO.getCreateDate() != null && logDTO.getUpdateTime() != null) {
                QueryWrapper<Log> queryWrapper = getQW(logDTO);
                targetService.page(page, queryWrapper);
            } else {
                QueryWrapper<Log> wrapper = new QueryWrapper<Log>();
                wrapper.orderBy(true, false, "create_date");
                targetService.page(page, wrapper);
            }
            return new ResponseBean(CommonResultCode.SUCCESS.getCode(), "系统日志分页查询" + CommonResultCode.SUCCESS.getMsg(), page);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("系统日志分页查询异常：{}", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(), "系统日志分页查询" + CommonResultCode.FAIL.getMsg());
        }
    }

    private QueryWrapper<Log> getQW(LogDTO logDTO) {
        QueryWrapper<Log> queryWrapper = new QueryWrapper<Log>();
        if (logDTO.getCreateDate() != null) {
            queryWrapper.lambda().ge(Log::getCreateDate, logDTO.getCreateDate()+ " 00:00:00");
        }
        if (logDTO.getUpdateTime() != null) {
            queryWrapper.lambda().lt(Log::getUpdateTime, logDTO.getUpdateTime()+ " 23:59:59");
        }
        return queryWrapper;
    }


    /**
     * 系统日志获取全部数据
     *
     * @return ResponseBean
     * @author cg
     * @date 2019-11-25
     * @since cloud2.0
     */
    @GetMapping("/all")
    @ApiOperation(value = "系统日志获取全部数据", notes = "系统日志获取全部数据")
    public ResponseBean findAll() {
        try {
            List<Log> models = targetService.list();
            return new ResponseBean(CommonResultCode.SUCCESS.getCode(), "系统日志获取全部数据" + CommonResultCode.SUCCESS.getMsg(), models);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("系统日志获取全部数据异常：{}", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(), "系统日志获取全部数据" + CommonResultCode.FAIL.getMsg());
        }
    }

    /**
     * 系统日志根据ID查找数据
     *
     * @param id 唯一编码
     * @return ResponseBean
     * @author cg
     * @date 2019-11-25
     * @since cloud2.0
     */
    @GetMapping("/find")
    @ApiOperation(value = "系统日志根据ID查找数据", notes = "系统日志根据ID查找数据")
    @ApiImplicitParam(required = true, name = "id", value = "唯一编码", dataType = "Long")
    public ResponseBean find(@RequestParam("id") Long id) {
        try {
            Log log = targetService.getById(id);
            return new ResponseBean(CommonResultCode.SUCCESS.getCode(), "系统日志根据ID查找数据" + CommonResultCode.SUCCESS.getMsg(), log);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("系统日志根据ID查找数据异常：{}", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(), "系统日志根据ID查找数据" + CommonResultCode.FAIL.getMsg());
        }
    }

    /**
     * 系统日志保存数据
     *
     * @param log 系统日志
     * @return ResponseBean
     * @author cg
     * @date 2019-11-25
     * @since cloud2.0
     */
    @PostMapping("/save")
    @ApiOperation(value = "系统日志保存数据", notes = "系统日志保存数据")
    @ApiImplicitParam(required = true, name = "log", value = "系统日志", dataType = "Log对象")
    public ResponseBean save(@RequestBody Log log) {
        try {
            targetService.save(log);
            return new ResponseBean(CommonResultCode.SUCCESS.getCode(), "系统日志保存数据" + CommonResultCode.SUCCESS.getMsg(), log);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("系统日志保存数据：{}", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(), "系统日志保存数据" + CommonResultCode.FAIL.getMsg());
        }
    }

    /**
     * 系统日志更新数据
     *
     * @param log 系统日志
     * @return ResponseBean
     * @author cg
     * @date 2019-11-25
     * @since cloud2.0
     */
    @PutMapping("/update")
    @ApiOperation(value = "系统日志更新数据", notes = "系统日志更新数据")
    @ApiImplicitParam(required = true, name = "log", value = "系统日志", dataType = "Log对象")
    public ResponseBean update(@RequestBody Log log) {
        try {
            targetService.updateById(log);
            return new ResponseBean(CommonResultCode.SUCCESS.getCode(), "系统日志更新数据" + CommonResultCode.SUCCESS.getMsg(), log);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("系统日志更新数据：{}", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(), "系统日志更新数据" + CommonResultCode.FAIL.getMsg());
        }
    }

    /**
     * 系统日志删除数据
     *
     * @param ids 批量删除数据
     * @return ResponseBean
     * @author cg
     * @date 2019-11-25
     * @since cloud2.0
     */
    @GetMapping("/del")
    @ApiOperation(value = "系统日志删除数据", notes = "系统日志删除数据")
    @ApiImplicitParam(required = true, name = "ids", value = "批量删除数据", dataType = "List<Long>")
    public ResponseBean del(String ids) {
        try {
            String[] ss = ids.split(",");
            for (int i = 0; i < ss.length; i++) {
                targetService.removeById(ss[i]);
            }
            return new ResponseBean(CommonResultCode.SUCCESS.getCode(), "系统日志删除数据" + CommonResultCode.SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("系统日志删除数据：{}", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(), "系统日志删除数据" + CommonResultCode.FAIL.getMsg());
        }
    }
}
